package com.chinatechstar.file.service.impl;

import java.util.*;

import com.chinatechstar.admin.entity.SysDict;
import com.chinatechstar.admin.entity.SysUser;
import com.chinatechstar.admin.mapper.SysUserMapper;
import com.chinatechstar.admin.service.SysDictService;
import com.chinatechstar.component.commons.utils.CurrentUserUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chinatechstar.file.entity.SysFileBusiness;
import com.chinatechstar.file.mapper.SysFileBusinessMapper;
import com.chinatechstar.file.service.SysFileBusinessService;
import com.chinatechstar.component.commons.result.PaginationBuilder;
import com.chinatechstar.component.commons.utils.SequenceGenerator;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * 业务逻辑实现层
 */
@Service
@Transactional
public class SysFileBusinessServiceImpl implements SysFileBusinessService {

    private final Logger logger = LoggerFactory.getLogger(getClass());
    private static final SequenceGenerator SEQUENCE_GENERATOR = new SequenceGenerator();

    @Autowired
    private SysFileBusinessMapper sysFileBusinessMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysDictService sysDictService;

    /**
     * 查询所有数据
     */
    @Override
    public List<SysFileBusiness> findSysFileBusinessAll() {
        return sysFileBusinessMapper.findSysFileBusinessAll();
    }

    /**
     * 查询分页
     */
    @Override
    public Map<String, Object> findSysFileBusinessByCondition(Integer currentPage, Integer pageSize, String name, String sorter) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("name", name);
        if (StringUtils.isNotBlank(sorter)) {
            String sort = sorter.substring(0, sorter.lastIndexOf('_'));
            String sequence = "ascend".equals(sorter.substring(sorter.lastIndexOf('_') + 1)) ? "ASC" : "DESC";
            paramMap.put("sort", sort);
            paramMap.put("sequence", sequence);
        } else {
            paramMap.put("sort", "createTime");
            paramMap.put("sequence", "DESC");
        }
        Page<Object> page = PageHelper.startPage(currentPage, pageSize);
        List<LinkedHashMap<String, Object>> resultList = sysFileBusinessMapper.findSysFileBusinessByCondition(paramMap);
        return PaginationBuilder.buildResult(resultList, page.getTotal(), currentPage, pageSize);
    }

    /**
     * 查询导出数据列表
     */
    @Override
    public List<LinkedHashMap<String, Object>> findSysFileBusinessForExcel(Map<String, Object> paramMap) {
        return sysFileBusinessMapper.findSysFileBusinessByCondition(paramMap);
    }

    /**
     * 根据主键ID数据查询数据
     */
    @Override
    public SysFileBusiness findSysFileBusinessById(Long id) {
        return sysFileBusinessMapper.findSysFileBusinessById(id);
    }

    /**
     * 根据主键ID数组查询数据
     */
    @Override
    public List<SysFileBusiness> findSysFileBusinessByIds(Long[] ids) {
        return sysFileBusinessMapper.findSysFileBusinessByIds(ids);
    }



    /**
     * 根据 fb_id 查询当前文件的工作流状态数据
     */
    @Override
    public LinkedHashMap<String, Object> findFileWorkflowStatusByBusinessId(Long fbId) {
        return sysFileBusinessMapper.findFileWorkflowStatusByBusinessId(fbId);
    }

    /**
     * 新增
     */
    @Override
    public void insertSysFileBusiness(SysFileBusiness sysFileBusiness) {
        SysUser currUser = sysUserMapper.getUserByUsername(CurrentUserUtils.getOAuth2AuthenticationInfo().get("name"));

        sysFileBusiness.setId(SEQUENCE_GENERATOR.nextId());
        sysFileBusinessMapper.insertSysFileBusiness(sysFileBusiness);
        logger.info("已新增： {}", sysFileBusiness.getId());
    }

    /**
     * 批量新增
     */
    @Override
    public void insertSysFileBusinesses(List<SysFileBusiness> sysFileBusinesses) {
        SysUser currUser = sysUserMapper.getUserByUsername(CurrentUserUtils.getOAuth2AuthenticationInfo().get("name"));

        for (SysFileBusiness sysFileBusiness : sysFileBusinesses) {
            sysFileBusiness.setId(SEQUENCE_GENERATOR.nextId());
        }
        sysFileBusinessMapper.insertSysFileBusinesses(sysFileBusinesses);
    }

    /**
     * 编辑
     */
    @Override
    public void updateSysFileBusiness(SysFileBusiness sysFileBusiness) {
        sysFileBusinessMapper.updateSysFileBusiness(sysFileBusiness);
        logger.info("已编辑： {}", sysFileBusiness.getId());
    }

    /**
     * 批量编辑
     */
    @Override
    public void updateSysFileBusinesses(List<SysFileBusiness> sysFileBusinesses) {
        sysFileBusinessMapper.updateSysFileBusinesses(sysFileBusinesses);
    }

    /**
     * 删除
     */
    @Override
    public void deleteSysFileBusinessById(Long id) {
        sysFileBusinessMapper.deleteSysFileBusinessById(id);
    }

    /**
     * 批量删除
     */
    @Override
    public void deleteSysFileBusinessByIds(Long[] ids) {
        sysFileBusinessMapper.deleteSysFileBusinessByIds(ids);
    }

}
